<template>
  <div class="space-category-analyze">
    <a-card title="图库分类占用">
      <v-chart :option="options" style="height: 400px;width: 80%;" :loading="loading" />
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { listSpaceCategoryAnalyzeUsingPost } from '@/api/spaceAnalysisController.ts'
import VChart from 'vue-echarts'
import 'echarts'

interface Props {
  queryPublic?: boolean
  queryAll?: boolean
  spaceId?: number | string
}

const props = withDefaults(defineProps<Props>(), {
  queryPublic: false,
  queryAll: false,
})

// 获取使用情况
const dataList = ref<API.SpaceCategoryAnalyzeResponse[]>([])

const loading = ref(false)
const getDataList = async () => {
  loading.value = true

  const res = await listSpaceCategoryAnalyzeUsingPost({
    spaceId: props.spaceId as number,
    queryPublic: props.queryPublic,
    queryAll: props.queryAll,
  })
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data ?? []
  }
  loading.value = false
}
const options = computed(() => {
  const categories = dataList.value?.map((item) => item?.category)
  const countData = dataList.value?.map((item) => item?.totalCount)
  const sizeData = dataList.value?.map((item) => (item?.totalSize / (1024 * 1024)).toFixed(2)) // 转为 MB

  return {
    tooltip: { trigger: 'axis' },
    legend: { data: ['图片数量', '图片总大小'], top: 'bottom' },
    xAxis: { type: 'category', data: categories },
    yAxis: [
      {
        type: 'value',
        name: '图片数量',
        axisLine: { show: true, lineStyle: { color: '#5470C6' } }, // 左轴颜色
      },
      {
        type: 'value',
        name: '图片总大小 (MB)',
        position: 'right',
        axisLine: { show: true, lineStyle: { color: '#91CC75' } }, // 右轴颜色
        splitLine: {
          lineStyle: {
            color: '#91CC75', // 调整网格线颜色
            type: 'dashed', // 线条样式：可选 'solid', 'dashed', 'dotted'
          },
        },
      },
    ],
    series: [
      { name: '图片数量', type: 'bar', data: countData, yAxisIndex: 0 },
      { name: '图片总大小', type: 'bar', data: sizeData, yAxisIndex: 1 },
    ],
  }
})

onMounted(() => {
  getDataList()
})
</script>

<style scoped lang="scss"></style>
